﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Abyss.Communicator;

namespace GunCommnicatorTest
{
    public partial class GunCommnicatorForm : Form
    {
        GunCommunicator m_Gun;
        Timer m_Timer;
        double m_Angle;

        public GunCommnicatorForm()
        {
            InitializeComponent();
            GetAvailablePorts();

            m_FireComboBox.Items.Add(bool.FalseString);
            m_FireComboBox.Items.Add(bool.TrueString);

            //m_Timer = new Timer();
            //m_Timer.Tick += m_Timer_Tick;
            //m_Timer.Interval = 30;
            //m_Timer.Start();
            //m_Angle = 0;
        }

        //void m_Timer_Tick(object sender, EventArgs e)
        //{
        //    if (m_Gun != null)
        //    {
        //        m_Angle += 0.5;
        //        m_Gun.ChangeGunAngleAndFire(m_Angle, 0, false);
        //    }
        //}

        private void GetAvailablePorts()
        {
            m_PortsList.Items.AddRange(GunCommunicator.GetAvailablePorts());
        }

        private void m_SendRequest_Click(object sender, EventArgs e)
        {
            try
            {
                double xAxis = double.Parse(m_XAxisBox.Text);
                double yAxis = double.Parse(m_YAxisBox.Text);
                bool isFire = false;

                if (bool.Parse((string)m_FireComboBox.SelectedItem))
                {
                    isFire = true;
                }
                
                m_Gun.ChangeGunAngleAndFire(xAxis, yAxis, isFire);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }
            
        }

        private void m_SelectPort_Click(object sender, EventArgs e)
        {
            bool isPortValid = true;
            try
            {
                m_Gun = new GunCommunicator((string)m_PortsList.SelectedItem);
            }
            catch
            {
                MessageBox.Show("Port isn't valid");
                isPortValid = false;
            }

            if (isPortValid)
            {
                m_SendRequest.Enabled = true;
            }
        }
    }
}
